import axios from 'axios';
import qs from 'query-string';
import type { DescData } from '@arco-design/web-vue/es/descriptions/interface';

export interface PolicyRecord {
  id: string;
  number: number;
  name: string;
  contentType: 'img' | 'horizontalVideo' | 'verticalVideo';
  filterType: 'artificial' | 'rules';
  count: number;
  status: 'online' | 'offline';
  createdTime: string;
}

export interface PolicyParams extends Partial<PolicyRecord> {
  page: number;
  limit: number;
  isDeleted: string;
}

export interface PolicyListRes {
  list: PolicyRecord[];
  total: number;
}

/**
 * choice
 */
export interface Choice {
  /**
   * 属于问题id
   */
  belongTo: string;
  content: string;
  /**
   * 跳转题目id列表
   */
  goTo?: null | string;
  id: null | string;
}

/**
 * question
 */
export interface Question {
  choices: Choice[];
  id: string;
  nextId: string;
  questionType: number;
  stem: string;
}

/**
 * survey
 */
export interface Survey {
  answerLimit?: number | null;
  description: string;
  endTime?: number | null;
  id: string;
  managedBy: string;
  name: string;
  questions: Question[];
  startTime?: number | null;
  typeId: string;
}

export interface isDeletedUser {
  userId: string[];
}
export function queryPolicyList(params: PolicyParams) {
  console.log('这里是用户列表');
  console.log(params);
  return axios.get<PolgeicyListRes>('/user/user/page', {
    params,
    paramsSerializer: (obj) => {
      return qs.stringify(obj);
    },
  });
}

// 恢复用户
export function recoverUser(data: isDeletedUser) {
  console.log('这里是恢复用户');
  console.log(data);
  return axios({
    url: '/user/user/recoverFromDelete',
    method: 'POST',
    data,
  });
}
// 硬删除用户
export function trueDeleteUser(data: isDeletedUser) {
  console.log('这里是硬删除用户');
  console.log(data);
  return axios({
    url: '/user/user',
    method: 'DELETE',
    data,
  });
}
// 软删除用户
export function fakeDeleteUser(data: isDeletedUser) {
  console.log('这里是软删除用户');
  console.log(data);
  return axios({
    url: '/user/user/logic',
    method: 'DELETE',
    data,
  });
}
// 群组列表
export function queryGroupList(params: PolicyParams) {
  console.log('查询群组列表');
  console.log();
  return axios.get<PolicyListRes>('/user/usergroup/page', {
    params,
    paramsSerializer: (obj) => {
      return qs.stringify(obj);
    },
  });
}

// 查看群组详情
export function getGroupDeatail(params) {
  console.log('查询群组详情');
  console.log(params);

  return axios.get('/user/usergroup/pageGroupUser', {
    params,
    paramsSerializer: (obj) => {
      return qs.stringify(obj);
    },
  });
}

// 修改群组
export function modifyGroupToBack(data) {
  console.log('修改群组');
  console.log(data);
  return axios.put('/user/usergroup', data);
  // return axios({
  //   url: '/user/usergroup',
  //   method: 'PUT',
  //   data,
  // });
}

// export function createSurvey(userid, data) {
//   // console.log("创建问卷");
//   // console.log(userid);
//   return axios({
//     url: '/survey/survey',
//     method: 'POST',
//     params: { userId: userid },
//     data,
//   });
//   // return axios.post('/survey/survey', params,data );
// }

// 创建群组的请求
export function createGroupApi(data) {
  console.log('-----------');

  console.log(data);

  return axios({
    url: '/user/usergroup',
    method: 'POST',
    data,
  });
}

export function questionList(params: PolicyParams) {
  console.log('这里是问题列表');
  console.log(params);
  return axios.get<PolicyListRes>('/survey/question/page', {
    params,
    paramsSerializer: (obj) => {
      return qs.stringify(obj);
    },
  });
}

export function deleteQuestion(data) {
  console.log(data);

  return axios({
    url: '/survey/question',
    method: 'DELETE',
    data,
  });
}
export function deleteSurvey(data) {
  console.log(data);

  return axios({
    url: '/survey/survey',
    method: 'DELETE',
    data,
  });
}

export function previewsurvey(data) {
  return axios.get<Survey>(`/survey/survey/${data}`, {
    // params: { id: data },
    paramsSerializer: (obj) => {
      return qs.stringify(obj);
    },
  });
}
export function answerRight(userId, surveyId) {
  console.log(`userId:${userId} surveyId:${surveyId}`);
  return axios({
    url: '/user/user/answerRight',
    method: 'GET',
    params: { userId, surveyId },
  });
}

export function surveyList(params: PolicyParams) {
  // console.log("这里是survey列表");

  // console.log(params);
  return axios.get<PolicyListRes>('/survey/survey/page', {
    params,
    paramsSerializer: (obj) => {
      return qs.stringify(obj);
    },
  });
}

export function createSurvey(userid, data) {
  // console.log("创建问卷");
  // console.log(userid);
  return axios({
    url: '/survey/survey',
    method: 'POST',
    params: { userId: userid },
    data,
  });
  // return axios.post('/survey/survey', params,data );
}

export function modifySurvey(data) {
  console.log(data);
  // console.log(userid);
  return axios({
    url: '/survey/survey',
    method: 'PUT',
    data,
  });
  // return axios.post('/survey/survey', params,data );
}
// 去找问题 根据id post请求
export function getQuestionDetail(data) {
  return axios({
    url: '/survey/question/getQuestions',
    method: 'POST',
    data,
  });
}

export interface ServiceRecord {
  id: number;
  title: string;
  description: string;
  name?: string;
  actionType?: string;
  icon?: string;
  data?: DescData[];
  enable?: boolean;
  expires?: boolean;
}
export function queryInspectionList() {
  return axios.get('/api/list/quality-inspection');
}

export function queryTheServiceList() {
  return axios.get('/api/list/the-service');
}

export function queryRulesPresetList() {
  return axios.get('/api/list/rules-preset');
}

// export function createQuestion(data) {
//   console.log("createQ");

//   return axios.post('/survey/question/addQuestion',data);
// }
export function answerSurvey(data) {
  return axios({
    url: '/survey/answer',
    method: 'POST',
    data,
  });
}

export function createQuestion(userid, data) {
  console.log('createQ');
  console.log(userid);
  return axios({
    url: '/survey/question/addQuestion',
    method: 'POST',
    params: { userId: userid },
    data,
  });
}

// 删除群组
// 后端实现了判断有用户不能删除
export function deleteGroup(data) {
  return axios({
    url: '/user/usergroup',
    method: 'DELETE',
    data,
  });
}

// 问卷统计
export function surveyStatics(data) {
  console.log('这里是问题列表');
  console.log(data);
  return axios.get(`/statistics/survey/${data}`, {
    // params: { data },
    paramsSerializer: (obj) => {
      return qs.stringify(obj);
    },
  });
}

// 答者统计
export function respondentStatics(data) {
  console.log('这里是问题列表');
  console.log(data);
  return axios.get(`/statistics/respondent/${data}`, {
    // params: { data },
    paramsSerializer: (obj) => {
      return qs.stringify(obj);
    },
  });
}

// 群组统计
export function groupStatics(data) {
  console.log('这里是问题列表');
  console.log(data);
  return axios.get(`/statistics/group/${data}`, {
    // params: { data },
    paramsSerializer: (obj) => {
      return qs.stringify(obj);
    },
  });
}
